using UnityEngine;
using System.Collections;

public class SpearmanAI : MonoBehaviour {

    public GameObject spear;
    private GameObject[] obs;
    private Vector3 moveDirection;
    public int moveSpeed = 1;
    
    private float timeLastFired = 1.0f;
    public float attackCooldown = 3.0f;
    
    private int attacktime = 10000;
    
    // Use this for initialization
    void Start()
    {
        obs = GameObject.FindGameObjectsWithTag("Player");
    }
    
    // Update is called once per frame
    void Update()
    {
        if(Network.isServer){
            GameObject closest = FindClosestEnemy(gameObject);
            transform.LookAt(closest.transform);
            // transform.rotation = Quaternion.Slerp(transform.rotation, closest.transform.rotation, Time.deltaTime * 0.3f);
            
            // don't do anything while we're attacking
            if (attacktime <= 50)
            {
                attacktime++;
            }
            
            // if we are done/not attacking
            if (attacktime > 50)
            {
                Vector3 diff = closest.transform.position - transform.position;
                float curDistance = diff.sqrMagnitude;
                curDistance =  Mathf.Sqrt(curDistance);
                
                // if we're really far away don't do anything
                if (curDistance > 200)
                    return;
                
                // to move into attack range
                if (curDistance > 30)
                {
                    moveDirection = transform.TransformDirection(Vector3.forward);
                    moveDirection.Normalize();
                    moveDirection = Vector3.Scale(moveDirection,new Vector3(moveSpeed,moveSpeed,moveSpeed));
                    this.transform.position += moveDirection;
                }

                if (timeLastFired <= Time.time && curDistance <= 60)
                {
                    moveDirection = Vector3.Scale(moveDirection,new Vector3(45,45,45));
                    GameObject spawn = Network.Instantiate(spear, this.transform.position + moveDirection, this.transform.rotation, 0) as GameObject;
                    spawn.transform.parent = this.transform;;
                    timeLastFired = Time.time + attackCooldown;
                    attacktime = 0;
                }
            }
            networkView.RPC("UpdateEnemyPosition",RPCMode.Server,transform.position,transform.rotation);
        }
    }
    
    // Find the closest enemy (I mean player)
    public static GameObject FindClosestEnemy(GameObject controlled)
    {
        GameObject[] gos;
        gos = GameObject.FindGameObjectsWithTag("Player");
        GameObject closest = null;
        float distance = Mathf.Infinity;
        Vector3 position = controlled.transform.position;
        foreach (GameObject go in gos)
        {
            Vector3 diff = go.transform.position - position;
            float curDistance = diff.sqrMagnitude;
            curDistance =  Mathf.Sqrt(curDistance);
            if (curDistance < distance)
            {
                closest = go;
                distance = curDistance;
            }
        }
    return closest;
    
    }
    [RPC]
    void UpdateEnemyPosition(Vector3 pos, Quaternion rot){
        transform.position = pos;
        transform.rotation = rot;
    }
}